<?php

//decode by http://www.yunlu99.com/
global $_GPC, $_W;
$ops = array("index", "export", "confirm", "refuse");
$op = in_array($_GPC["op"], $ops) ? $_GPC["op"] : "index";
if ($op == "index") {
	$pindex = max(1, intval($_GPC["page"]));
	$psize = 10;
	$count = pdo_fetchcolumn("select count(id) from " . tablename("d1sj_card_cash") . " where uniacid=" . $_W["uniacid"]);
	$list = pdo_fetchall("select * from " . tablename("d1sj_card_cash") . " where uniacid=" . $_W["uniacid"] . " order by id desc  limit " . ($pindex - 1) * $psize . "," . $psize);
	$pager = pagination($count, $pindex, $psize);
}
if ($op == "export") {
	if (checksubmit()) {
		$status = $_GPC["status"];
		$start_date = strtotime($_GPC["start_date"]);
		$end_date = strtotime($_GPC["end_date"]);
		if ($status == 0) {
			$cash_result = pdo_fetchall("select ca.*,me.nickname from " . tablename("d1sj_card_cash") . " as ca left join " . tablename("mc_members") . " as me on ca.uid=me.uid " . " where ca.uniacid=:uniacid", array(":uniacid" => $_W["uniacid"]));
		} else {
			$cash_result = pdo_fetchall("select ca.*,me.nickname from " . tablename("d1sj_card_cash") . " as ca left join " . tablename("mc_members") . " as me on ca.uid=me.uid " . " where ca.status=:status and ca.uniacid=:uniacid and ca.createtime between :start_date and :end_date", array(":status" => $status, ":uniacid" => $_W["uniacid"], ":start_date" => $start_date, ":end_date" => $end_date));
		}
		if (!$cash_result) {
			message("对不起找到相应的数据", $this->createWebUrl("cash", array("op" => "index")), "error");
		}
		if ($cash_result) {
			$html = "﻿";
			$filter = array("nickname" => "用户昵称", "money" => "提现金额", "status" => "状态", "mobile" => "电话", "alipay" => "支付宝账号", "name" => "真实姓名", "createtime" => "创建时间");
			foreach ($filter as $key => $title) {
				$html .= $title . "\t,";
			}
			$html .= "\n";
			foreach ($cash_result as $k => $v) {
				$shuju[$k]["nickname"] = $v["name"];
				$shuju[$k]["money"] = $v["money"];
				if ($v["status"] == 1) {
					$shuju[$k]["status"] = "未处理";
				} else {
					if ($v["status"] == 2) {
						$shuju[$k]["status"] = "已经转款";
					} else {
						$shuju[$k]["status"] = "被拒绝";
					}
				}
				$shuju[$k]["mobile"] = $v["mobile"];
				$shuju[$k]["alipay"] = $v["alipay"];
				$shuju[$k]["name"] = $v["name"];
				$shuju[$k]["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
				foreach ($filter as $key => $title) {
					$html .= $shuju[$k][$key] . "\t,";
				}
				$html .= "\n";
			}
			header("Content-type:text/csv");
			header("Content-Disposition:attachment; filename=提现记录.csv");
			echo $html;
			die;
		}
	}
}
if ($_GPC["id"]) {
	$id = $_GPC["id"];
	$cash_res = pdo_fetch("select id,status,uid,money from" . tablename("d1sj_card_cash") . " where id=:id", array(":id" => $id));
	if (!$cash_res) {
		message("没有对应的数据", $this->createWebUrl("cash", array("op" => "index")), "error");
	}
	if ($op == "confirm") {
		$cash_date = array("status" => 2);
		$user_res = pdo_fetch("select id,uid,money from " . tablename("d1sj_card_user") . " where uid=:uid ", array(":uid" => $cash_res["uid"]));
		$up_user_money = $user_res["money"] - $cash_res["money"];
		if ($up_user_money < 0) {
			message("用户余额不足不能提现", $this->createWebUrl("cash", array("op" => "index")), "error");
		}
		$user_date = array("money" => $up_user_money);
		$user_result = pdo_update("d1sj_card_user", $user_date, array("id" => $user_res["id"]));
		$cash_result = pdo_update("d1sj_card_cash", $cash_date, array("id" => $id));
		if ($cash_result && $user_result) {
			message("修改成功", $this->createWebUrl("cash", array("op" => "index")), "success");
		}
	}
	if ($op == "refuse") {
		$cash_date = array("status" => 3);
		$cash_result = pdo_update("d1sj_card_cash", $cash_date, array("id" => $id));
		if ($cash_result) {
			message("修改成功", $this->createWebUrl("cash", array("op" => "index")), "success");
		}
	}
}
include $this->template("web/cash");